package com.atguigu.tms.mock.service.impl;

import com.atguigu.mock.util.RandomBox;
import com.atguigu.mock.util.RandomName;
import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomNumString;
import com.atguigu.tms.mock.bean.BaseOrgan;
import com.atguigu.tms.mock.bean.EmployeeInfo;
import com.atguigu.tms.mock.constant.TmsConstant;
import com.atguigu.tms.mock.mapper.EmployeeInfoMapper;
import com.atguigu.tms.mock.service.BaseComplexService;
import com.atguigu.tms.mock.service.EmployeeInfoService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 职员表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-03-20
 */
@Service
public class EmployeeInfoServiceImpl extends AdvServiceImpl<EmployeeInfoMapper, EmployeeInfo> implements EmployeeInfoService {

    @Autowired
    BaseComplexService baseComplexService;

    public EmployeeInfo initEmployeeInfo(Long orgId, String positionType){

        EmployeeInfo employeeInfo = genEmployeeInfo(positionType);
        saveOrUpdate(employeeInfo,true);
        return  employeeInfo;
    }





    public List<EmployeeInfo> initEmployeeInfo(Integer num, String positionType){

        List<EmployeeInfo> employeeInfoList=new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            EmployeeInfo employeeInfo = genEmployeeInfo(positionType);
            employeeInfoList.add(employeeInfo);

        }
        this.saveOrUpdateBatch(employeeInfoList,1000,true);
        return employeeInfoList;

    }

    @Override
    public List<EmployeeInfo> initEmployeeInfo(List<BaseOrgan> baseOrganList, String positionType) {
        List<EmployeeInfo> employeeInfoList=new ArrayList<>(baseOrganList.size());
        for (BaseOrgan baseOrgan : baseOrganList) {
            EmployeeInfo employeeInfo = genEmployeeInfo(positionType);
            employeeInfo.setOrgId(baseOrgan.getId());
            employeeInfoList.add(employeeInfo);
        }

        this.saveOrUpdateBatch(employeeInfoList,1000,true);
        return employeeInfoList;
    }

    private  EmployeeInfo genEmployeeInfo(String positionType){
        EmployeeInfo employeeInfo = new EmployeeInfo();
        String idCardPrefix = RandomNumString.getRandNumString(0, 9, 6, "", true);
        Date birtyday = DateUtils.addMonths(new Date(), -1 * RandomNum.getRandInt(15 * 12, 55 * 12));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String birtydaystr = simpleDateFormat.format(birtyday);

        String idCardPostfix = RandomNumString.getRandNumString(0, 9, 2, "", true);
        String gender = new RandomBox("F", "M").getRandStringValue();
        employeeInfo.setGender(gender);
        String idCardGenderfix ="3";
        if(gender.equals("F")) {
              idCardGenderfix ="4";
        }
        String idCardLastfix = RandomNumString.getRandNumString(0, 9, 1, "", true);

        employeeInfo.setIdCard(idCardPrefix+birtydaystr+idCardPostfix+idCardGenderfix+idCardLastfix);
        employeeInfo.setBirthday(birtyday);

        employeeInfo.setEmploymentDate(DateUtils.addMonths(new Date(),  -1* RandomNum.getRandInt(1 * 12, 5 * 12)));
        employeeInfo.setGraduationDate(DateUtils.addMonths(employeeInfo.getEmploymentDate(), -1 * RandomNum.getRandInt(3 * 12, 10 * 12)));

        String lastName = RandomName.insideLastName(gender);
        employeeInfo.setRealName(RandomName.getChineseFamilyName()+lastName);
        employeeInfo.setUsername( RandomStringUtils.random(12,true,true));
        employeeInfo.setPassword(RandomStringUtils.random(8,true,true));
        employeeInfo.setIsDeleted("0");
        employeeInfo.setCreateTime(employeeInfo.getEmploymentDate());
        employeeInfo.setPhone("13"+ RandomNumString.getRandNumString(1,9,9,""));
        employeeInfo.setEducation(new RandomBox(TmsConstant.EDU_JUNIOR,TmsConstant.EDU_HIGH,TmsConstant.EDU_SPECIALTY,TmsConstant.EDU_BACHELOR).getRandStringValue());
        employeeInfo.setPositionType(positionType);

        return employeeInfo;
    }

    public static void main(String[] args) {
        System.out.println(RandomStringUtils.random(12,true,true));
    }
}
